﻿define('ui/controller/searchController', 
	['ui/controller/itemController', 'ui/controller/conditionController',
	 'ui/controller/panelController', 'ui/controller/areaLevelController',
	 'ui/controller/tradeLevelController', 'ui/controller/controlLevelController',
	 'ui/controller/advancedFilterController', 'ui/markerList/enterpriseML',
	 'ui/markerList/cleanML', 'ui/card/layerCard'], 
	function(itemController, conditionController, 
			panelController, areaLevelController,
			tradeLevelController, controlLevelController, 
			advancedFilterController, enterpriseML,
			cleanML, layerCard) {

	/**
	 * 搜索框：包含比较复杂的交互逻辑
	 * 1、关键字
	 * 2、取消图标
	 */
	var searchController = {
		
		//关键字搜索框
		$keywords: $('#sole-input'),
		//取消图标
		$cancel: $(".input-clear"),
		//搜索按钮
		$searrchBtn: $("#search-button"),
			
		/**
		 * 初始化
		 * 1、获取地图焦点
		 * 2、控制为空值
		 * 3、绑定搜索框点击事件
		 * 4、绑定搜索框回车键事件
		 */
		initial: function() {
			searchController.$keywords.focus();
			searchController.$keywords.val('');
			
		},
		
		eventBind: function() {
			console.log("searchController eventBind......");
			searchController.onClick();
			searchController.onKeyPress();
			searchController.onCancelIconClick();
			searchController.onSearchButtonClick();
		},
		
		/**
		 * 输入框点击事件：如果为空显示专题选项卡
		 */
		onClick: function() {
			searchController.$keywords.click(function(){
				if($(this).val()==undefined || $(this).val()==""){
					itemController.showItemBox();
				}
			});
		},
		
		/**
		 * 输入框回车事件：搜索企业、排口，目前只实现了企业搜索
		 */
		onKeyPress: function() {
			searchController.$keywords.on('keypress', function(e) {
		        if (e.which === 13) {
		        	searchController.doSearch();
		        }
		    });
			
			searchController.$keywords.keydown(function(e){
				  if (e.keyCode === 8 && searchController.$keywords.val().length==1) {
			        	searchController.setValue('');
			     		searchController.hideCancelIcon();
			     		itemController.showItemBox();
			     		conditionController.hideOuterBox();
			     		conditionController.changeShowStatus("enterprise");
			     		conditionController.render();
			     		panelController.hidePanel();
			     		
			     		//清空markerList及地图
						cleanML.cleanAllMarkerList();
						layerCard.closeAll();
			        }
			});
		},
		
		doSearch: function() {
			enterpriseML.reRenderData();
            
            searchController.setValue(searchController.$keywords.val());
    		searchController.showCancelIcon();
    		itemController.hideItemBox();
    		conditionController.showOuterBox();
    		conditionController.changeShowStatus("enterprise");
    		conditionController.render();
    		panelController.showPanel();
		},
		
		/**
		 * 搜索按钮点击事件
		 */
		onSearchButtonClick: function() {
			searchController.$searrchBtn.click(function(){
				searchController.doSearch();
			});
		},
		
		/**
		 * 绑定取消图标点击事件
		 */
		onCancelIconClick: function() {
			searchController.$cancel.click(function(){
				searchController.onCancelIconClear();
			});
		},
		
		/**
		 * 显示取消图标
		 */
		showCancelIcon: function() {
			searchController.$cancel.css({"display":"block"});
		},
		
		/**
		 * 隐藏取消图标
		 */
		hideCancelIcon:function(){
			searchController.$cancel.css({"display":"none"});
		},
		
		/**
		 * 取消图标点击事件
		 * 1、隐藏图标
		 * 2、初始化：搜索框
		 * 3、初始化：专题选项卡
		 * 4、初始化：选择条件栏
		 * 5、初始化：地区层级、行业层级、监管类型、高级筛选
		 * 6、初始化：面板列表
		 * 7、清空地图：MarkerList
		 * 8、隐藏所有选项卡：企业汇总、排口汇总、排口详情、排放量排名等
		 */
		onCancelIconClear: function() {
			searchController.hideCancelIcon();
			searchController.initial();
			itemController.initial();
			conditionController.initial();
			panelController.initial();
			areaLevelController.initial();
			tradeLevelController.initial();
			controlLevelController.initial();
			advancedFilterController.initial();
			
			itemController.showItemBox();
			
			//清空markerList及地图
			cleanML.cleanAllMarkerList();
			layerCard.closeAll();
		},
		
		setValue: function(val){
			searchController.$keywords.val(val);
		}
	};

	
	return searchController;
});